<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends BaseController {
	
	function index(){
		$alias = "a";
		$join = "LEFT JOIN __ROLE__ r ON a.role_id = r.id";
		$field = "a.*,IFNULL(r.name, '<font color=\"red\">未分配角色</font>') role";
		$this -> _list('admin', $where, $order, $alias, $field, $join);
	}
	
	function changePwd(){
		$user = $_SESSION['admin'];
		if (empty($user)){
			$this -> error('请登陆后操作!', U('Index/login'));
			exit;
		}
		if ($_POST['oldpwd'] && $_POST['password'] && $_POST['repassword']){
			if($_POST['password'] != $_POST['repassword']){
				$this->error("两次输入密码不一致");
			}
			$adminObj = M('Admin');
			$saveDate['password'] = xmd5($_POST['password']);
			$maps['id'] = $user['id'];
			$maps['password'] = xmd5($_POST['oldpwd']);
			if(!!$re = $adminObj->where($maps)->find()){
				if ($adminObj->where("id={$user['id']}")->save($saveDate)){
					$this->success('修改成功');
				}else{
					$this->error("修改失败");
				}
			}else{
				$this->error("原始密码错误");
			}
		}else{
			$this->display();
		}
	}
	
	function del(){
		$this -> _del('admin', I("id"));
		$this -> success('操作成功！', $_SERVER['HTTP_REFERER']);
	}
	
	function edit(){
		if(IS_POST){
			$password=$_POST['password'];
			$repassword=$_POST['repassword'];
			if(empty($password)){
				unset($_POST['password']);
				unset($_POST['repassword']);
			}elseif($password == $repassword){
				$_POST['password'] = xmd5($password);
				unset($_POST['repassword']);
			}
			
		}else{
			$roleObj = M("Role");
			$roleList = $roleObj->select();
			$this->assign('roleList', $roleList);
		}
		$this -> _edit('admin', U('index'));
	}
	/**
	 * 禁用/启用用户
	 */
	function disabled(){
		$id = $_GET['id'];
		$adminObj = M('Admin');
		$user=$adminObj->find($id);
		if(empty($user)){
			$this->error("用户不存在");
		}
		
		$is_disabled = $_GET['is_disabled'];
		$saveData['is_disabled'] = $is_disabled;
		$maps['id'] = $id;
		$adminObj->where($maps)->save($saveData);
		$this->success("操作成功");
	}
}

?>